<?php

namespace app\common\model\product;

use think\facade\Cache;
use app\common\model\BaseModel;

/**
 * 充值模板模型
 */
class RechargeTemp extends BaseModel
{
    protected $pk = 'recharge_template_id';
    protected $name = 'recharge_temp';

    /**
     * 中间表
     * @return \think\model\relation\BelongsToMany
     */
    public function rechargeTempDictType()
    {
        return $this->hasMany('app\\common\\model\\product\\RechargeTempDictType', "recharge_template_id","recharge_template_id")->order("sort_order asc");
    }

    /**
     * 详情
     */
    public static function detail($category_id)
    {
        return (new static())->find($category_id);
    }

    /**
     * 获取商品列表
     */
    public function getList($param)
    {
        // 商品列表获取条件
        $params = array_merge([
        ], $param);

        // 筛选条件
        $filter = [];
        $model = $this;
        if (!empty($params['template_name'])) {
            $model = $model->where('template_name', 'like', '%' . trim($params['template_name']) . '%');
        }

        $list = $model
            ->field(['*'])
            ->with(["rechargeTempDictType"])
            ->where($filter)
            ->order("recharge_template_id desc")
            ->paginate($params);
        $rechargeDictType = new RechargeDictType;
        foreach ($list as $value){
            $value["dict_name_str"] = implode(",",array_map(function ($item) use ($rechargeDictType){
                $dict = $rechargeDictType->detail($item["dict_id"]);
                return $dict["dict_name"];
            }, $value->rechargeTempDictType->toArray()));
        }

        // 整理列表数据并返回
        return $list->hidden(['rechargeTempDictType']);

    }


}
